<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class AlbumController extends BaseController {

	//ThinkPHP的构造函数
	public function _initialize() {
		//自动加载的类
		parent::_initialize();
		new \Org\Admin_autoload();
	}

    public function index(){
    	$this->assign('page_title','相册列表');
    	$DataModel = D("Album");
    	$items = $DataModel->select();
    	$this->assign('volist',$items);
    	
    	//$this->assign('')
        $this->display();
    }
    
    public function add(){
    	if(!empty($_POST)) {
    	    $DataModel = D("Album");

    		if(!empty($_POST['images_url'])){

                $base_url=dirname(dirname(dirname(dirname(__FILE__)))).'/www/';
                $base_img_url=$base_url.'Public/Uploads/Album/';
                $image_url= $base_url.$_POST['images_url'];
				$image_url=str_replace('//','/',$image_url);
				//echo $image_url.'<br />';exit;
                $image= new \Think\Image();
                $image->open($image_url);
                $large_thmumb= time().'_large'.'.jpg';
                $small_thmumb= time().'_small'.'.jpg';
                $max_thmumb= time().'_max'.'.jpg';
				//生成缩略图
				$image->thumb(209, 135)->save($base_img_url.$small_thmumb);
				$image->open($image_url);	//生成缩略图之后要重新打开
				$image->thumb(550, 410)->save($base_img_url.$large_thmumb);
				$image->open($image_url);
				$image->thumb(1024, 768)->save($base_img_url.$max_thmumb);
				//存入数据库的值
				$_POST['small_thumb']= '/Public/Uploads/Album/'.$small_thmumb;
				$_POST['large_thumb']= '/Public/Uploads/Album/'.$large_thmumb;
				$_POST['imgurl']= '/Public/Uploads/Album/'.$max_thmumb;
    		}

    		$_POST['add_time'] = date('Y-m-d H:i:s');
    		$_POST['is_show'] = 1;


    		$data = $DataModel->create($_POST);
    		$insert_id = $DataModel->add($data);
    		if($insert_id){

    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>window.location.href='".__APP__."/index.php/Album/';</script>";exit;
    		}
    	}else{
    	    $CateModel = D("AlbumCategory");
    	    $category_list = $CateModel->select();
    		$this->assign('category_list',$category_list);
    	    $this->display();
    	}
    }
    
    public function getData(){
    	$CateModel = D("AlbumCategory");
    	$category_list = $CateModel->select();
    	$categories = array();
    	foreach($category_list as $v){
    		$categories[$v['id']] = $v['name'];
    	}
    	$DataModel = D("Album");
    	$condition = array();
    	if(!empty($_GET['id'])){
    		$condition[] = "id=".(int)$_GET['id'];
    	}
    	if(!empty($_GET['name'])){
    		$condition[] = "name LIKE '%".$_GET['name']."%'";
    	}
    	if(!empty($_GET['sort'])){
    		$condition[] = 'sort = '.(int)$_GET['sort'];
    	}
    	$conditions = implode(" AND ",$condition);
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->where($conditions)->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->where($conditions)->page($page)->limit($limit)->order('id desc')->select();
    	 
    	for($i=0;$i<count($list);$i++){
    		$list[$i]['category'] = $categories[$list[$i]['category_id']];
			$list[$i]['imgurl']='http://'.FRONT_SITE_NAME.$list[$i]['imgurl'];
    	}
    	$items['rows'] = $list;
    	$this->ajaxReturn($items);
    }
    
    
    public function edit(){
    	$DataModel = D("Album");
    	if(!empty($_POST)) {
    		$_POST['add_time'] = date('Y-m-d H:i:s');
			//增加缩略图 by lijianjun 20170213
            if(!empty($_POST['images_url'])){  //hyy  重新处理图片 2017.3.8
				//echo $_POST['images_url'];exit;
                $base_url=dirname(dirname(dirname(dirname(__FILE__)))).'/www/';
                $base_img_url=$base_url.'Public/Uploads/Album/';
                $image_url= $base_url.$_POST['images_url'];
				$image_url=str_replace('//','/',$image_url);
				//echo $image_url.'<br />';
                $image= new \Think\Image();
                $image->open($image_url);
                $large_thmumb= time().'_large'.'.jpg';
                $small_thmumb= time().'_small'.'.jpg';
				$max_thmumb= time().'_max'.'.jpg';
				//生成缩略图
				$image->thumb(209, 135)->save($base_img_url.$small_thmumb);
				$image->open($image_url);	//生成缩略图之后要重新打开
				$image->thumb(550, 410)->save($base_img_url.$large_thmumb);
				$image->open($image_url);
				$image->thumb(1024, 768)->save($base_img_url.$max_thmumb);
				//存入数据库的值
                $_POST['small_thumb']= '/Public/Uploads/Album/'.$small_thmumb;
                $_POST['large_thumb']= '/Public/Uploads/Album/'.$large_thmumb;
                $_POST['imgurl']= '/Public/Uploads/Album/'.$max_thmumb;
				//删除原空间图
                $pic= $DataModel->where("id=".(int)$_POST['id'])->find();
                unlink($base_url.$pic['small_thumb']);
                unlink($base_url.$pic['large_thumb']);
                unlink($base_url.$pic['imgurl']);
            }
			//更新数据表
            $DataModel->where("id=".(int)$_POST['id'])->save($_POST);
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>window.location.href='".__APP__."/index.php/Album/';</script>";exit;
    	}else{
    		$item = $DataModel->where("id=".(int)$_GET['id'])->select();
    		$this->assign('vo',$item[0]);
    		$CateModel = D("AlbumCategory");
    		$category_list = $CateModel->select();
    		$this->assign('category_list',$category_list);
    		$this->display();
    	}
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D("Album");
    		$id = $_POST['id'];
    		$item =  $DataModel->where("id=".(int)$id)->find();
    		$result = $DataModel->delete((int)$id);
    		
    		if($result){
    			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    			unlink($root_path."/www".str_replace("http://".FRONT_SITE_NAME,"",$item['imgurl']));
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }
       
    
}